import { Component, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { ResetForm } from "./reset-pass.class";
import { ResetPassService } from "./reset-pass.service";

@Component({
  selector: 'reset-pass',
  templateUrl: 'reset-pass.component.html',
  styleUrls: ['reset-pass.scss'],
  providers: [ResetPassService]
})
export class ResetPassComponent implements OnInit {
  private resetUrl: string = '/platform/user/findPassword';
  private directToBase: string = '/login';
  private resetForm: ResetForm;
  private newPass: string;
  private errorMsg: string;
  private loading = false;

  constructor(private _service: ResetPassService,
              private _router: Router) {
  }

  ngOnInit() {
    this.resetForm = new ResetForm();
  }

  onSubmit() {
    this.loading = true;
    this._service.submit(this.resetUrl, this.resetForm).then(res => {
        if (res.status === 'success') {
          this.loading = false;
          this.newPass = res.data.newpwd;
        } else {
          this.loading = false;
          this.errorMsg = res.data ? res.data : "网络可能发生错误 稍后再试";
        }
      },
      error => {
        this.errorMsg = error;
      }
    );
  }
}
